package com.xingkeduo.business.operation.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.operation.domain.VendorDomain;
import com.xingkeduo.pojo.json.VendorJson;
import com.xingkeduo.pojo.query.VendorQuery;
import com.xingkeduo.pojo.table.VendorTable;
import com.xingkeduo.util.pojo.base.BaseController;
import com.xingkeduo.util.tool.ReturnUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static com.xingkeduo.tool.OperaConstants.CMS_URL_PREFIX;

/**
 * 大区管理
 *
 * @author wangxilong <wangxilong@xingkeduo.com>
 * @version id:v0.1
 * @package BeautyOS
 * @copyright Copyright (c) XKD 2019
 */
@Slf4j
@RestController
@RequestMapping(CMS_URL_PREFIX +"/vendor")
public class VendorController extends InitController {

    @Autowired
    private VendorDomain vendorDomain;

    @RequestMapping(value = "/{urlParam:.+}", method = RequestMethod.GET)
    public String doGet(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            VendorQuery vendorQuery = VendorJson.createQueryForVendor(urlParam);
            long id = vendorQuery.getId();
            VendorTable vendorTable = vendorDomain.getVendorInfoById(id);
            return returnUtil.getSuccess(VendorJson.toJson(vendorTable));
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //添加
    @RequestMapping(value = "", method = RequestMethod.POST)
    public String doPost(@RequestBody @Valid VendorTable vendorTable, ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            vendorTable.setCreatedBy(this.getOperator(request));
            log.info("添加大区接口请求参数:{}", JSON.toJSONString(vendorTable));
            int vendor = vendorDomain.createVendor(vendorTable);
            log.info("添加大区接口返回值:{}", vendor);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //修改
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.PUT)
    public String doPut(@PathVariable String urlParam, @RequestBody @Valid VendorTable vendorVo , ServerHttpRequest request) {
        returnUtil = new ReturnUtil();
        try {
            //this.errMsg(bindingResult);
            VendorTable vendorTable = VendorJson.createTableForModifyVendor(urlParam,vendorVo);
            vendorTable.setUpdatedBy(this.getOperator(request));
            log.info("修改大区接口请求参数:{}", JSON.toJSONString(vendorTable));
            int vendor = vendorDomain.modifyVendor(vendorTable);
            log.info("修改大区接口返回值:{}", vendor);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    //删除
    @RequestMapping(value = "/{urlParam}", method = RequestMethod.DELETE)
    public String doDelete(@PathVariable String urlParam) {
        returnUtil = new ReturnUtil();
        try {
            VendorQuery vendorQuery = VendorJson.createQueryForVendor(urlParam);
            long id = vendorQuery.getId();
            vendorDomain.delVendor(id);
            return returnUtil.getSuccess();
        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }

    @RequestMapping(value = "/lists", method = RequestMethod.GET)
    public String doGetLists() {
        returnUtil = new ReturnUtil();
        try {
            VendorQuery vendorQuery = new VendorQuery();
            List<VendorTable> vendorList= vendorDomain.getVendorlists(vendorQuery);

            return returnUtil.getSuccess(VendorJson.toJson(vendorList));

        } catch (Throwable throwable) {
            return dealException(throwable, returnUtil);
        }
    }
}
